<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Set property accessibility</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="reflectionproperty.isstatic.html">« ReflectionProperty::isStatic</a></li>
      <li style="float: right;"><a href="reflectionproperty.setvalue.html">ReflectionProperty::setValue »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.reflectionproperty.html">ReflectionProperty</a></li>
    <li>Set property accessibility</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="reflectionproperty.setaccessible" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">ReflectionProperty::setAccessible</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.3.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">ReflectionProperty::setAccessible</span> &mdash; <span class="dc-title">Set property accessibility</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-reflectionproperty.setaccessible-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>ReflectionProperty::setAccessible</strong></span>(<span class="methodparam"><span class="type">bool</span> <code class="parameter">$accessible</code></span>): <span class="type"><span class="type void">void</span></span></div>

  <p class="para rdfs-comment">
   Enables access to a protected or private property via the <span class="methodname"><a href="reflectionproperty.getvalue.html" class="methodname">ReflectionProperty::getValue()</a></span> and
   <span class="methodname"><a href="reflectionproperty.setvalue.html" class="methodname">ReflectionProperty::setValue()</a></span> methods.
  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <span class="simpara">
    As of PHP 8.1.0, calling this method has no effect; all properties are accessible by default.
   </span>
  </p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-reflectionproperty.setaccessible-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">accessible</code></dt>

     <dd>

      <p class="para">
       <strong><code>true</code></strong> to allow accessibility, or <strong><code>false</code></strong>.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-reflectionproperty.setaccessible-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   没有返回值。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-reflectionproperty.setaccessible-examples">
 <h3 class="title">范例</h3>
  <div class="example" id="example-5254">
   <p><strong>示例 #1 Simple Class definition</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">MyClass<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;</span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'bar'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$property&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">ReflectionProperty</span><span style="color: #007700">(</span><span style="color: #DD0000">"MyClass"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"foo"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$property</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setAccessible</span><span style="color: #007700">(</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$obj&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MyClass</span><span style="color: #007700">();<br />echo&nbsp;</span><span style="color: #0000BB">$property</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getValue</span><span style="color: #007700">(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">foo</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
bar
Fatal error: Uncaught Error: Cannot access private property MyClass::$foo in /in/WJqTv:12
</pre></div>
   </div>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-reflectionproperty.setaccessible-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="methodname"><a href="reflectionproperty.isprivate.html" class="methodname" rel="rdfs-seeAlso">ReflectionProperty::isPrivate()</a> - Checks if property is private</span></li>
    <li class="member"><span class="methodname"><a href="reflectionproperty.isprotected.html" class="methodname" rel="rdfs-seeAlso">ReflectionProperty::isProtected()</a> - Checks if property is protected</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>